package J2_29;

import java.lang.reflect.Array;

public class test {
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }
    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    /**
     * 求中位数的下标
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int middleNum(int[] array,int left,int right) {
        int mid = (left+right)/2;
        if(array[left] < array[right]) {
            if(array[mid] < array[left]) {
                return left;
            }else if(array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            //array[left] > array[right]
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    public static void insertSort(int[] array,int left,int right) {
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= left ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    private static int partition(int[] array, int left, int right) {
        int prev = left ;
        int cur = left+1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,cur,prev);
            }
            cur++;
        }

        swap(array,prev,left);
        return prev;
    }
    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }
        //System.out.println("start: "+start +" end: "+end);
        if(end - start + 1 <= 15) {
            insertSort(array, start, end);
            return;
        }

        //1 2 3 4 5 6 7
        int index = middleNum(array,start,end);
        swap(array,index,start);
        //4 2 3 1 5 6 7

        int pivot = partitionHoare(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int partitionHoare(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while (left < right) {
            // 单独的循环 不能一直减到超过最左边的边界
            while (left < right && array[left] <= tmp) {
                left++;
            }
            while (left < right && array[right] >= tmp) {
                right--;
            }


            swap(array,left,right);
        }
        swap(array,i,right);
        return left;
    }

    public static void main(String[] args) {
        int arr[] = {8,7,6,5,4,3,2,1};
        quickSort(arr);
        System.out.println("===");
    }

}
